
import { _decorator, Component, Node, systemEvent, SystemEventType, Touch, Vec2, PhysicsSystem, director } from 'cc';
import { Cat } from './cat';
import { ViewMgr } from './Mgr/ViewMgr';
import { Player } from './player';
import { GameView } from './ui/GameView';
const { ccclass, property } = _decorator;
enum MoveDirction {
    Left = -1,
    Right = 1,
}

@ccclass('GameContrl')
export class GameContrl extends Component {

    static instance: GameContrl;
    private startPos: Vec2 = new Vec2(0, 0);
    private endPos: Vec2 = new Vec2(0, 0);
    private touchOffset = 30;
    private moveDir = 0;

    public score: number = 0;




    @property(Cat) player: Cat = null!



    start() {
        // [3]
        // systemEvent.on()
        PhysicsSystem.instance.enable = true;


    }

    onLoad() {
        systemEvent.on(SystemEventType.TOUCH_START, this.onTouchStart, this);
        systemEvent.on(SystemEventType.TOUCH_END, this.onTouchEnd, this);
        ViewMgr.getInstance().openGame(this.node.scene)

        GameContrl.instance = this;

    }

    update() {

    }

    onTouchStart(touch: Touch) {
        if (Player.instance.isDeath) return;
        this.startPos = touch.getLocation();

    }

    onTouchEnd(touch: Touch) {
        if (Player.instance.isDeath) return;
        this.endPos = touch.getLocation();

        if (Math.abs((this.endPos.x - this.startPos.x)) > this.touchOffset) {
            if (this.startPos.x - this.endPos.x > 0) {
                this.moveDir = MoveDirction.Right
            } else {
                this.moveDir = MoveDirction.Left
            }
        }
        this.player.setDirction(this.moveDir);

    }

    eatFish() {
        this.score++;
        GameView.instance.eatFish(this.score);
    }

    revive() {
        this.player.animationCom.crossFade('run', 0.17);
        Player.instance.speed = 30;
        Player.instance.isDeath = false;

        //猫站起来。猫的速度改为初始速度,设置标志位，关闭障碍物的碰撞
    }


}


